package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.api.Urls
import com.onlyphoto.onlymall.bean.response.OrderDetailsBean
import com.onlyphoto.onlymall.mvp.view.activity.UploadPicActivity
import com.onlyphoto.onlymall.mvp.view.activity.WebViewActivity
import com.onlyphoto.onlymall.utils.StringUtil

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-08-26.
 */
class OrderDetailsFooterBinder : ItemViewBinder<OrderDetailsBean, OrderDetailsFooterBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_order_details_footer, parent, false)
        return ViewHolder(root)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, item: OrderDetailsBean, payloads: MutableList<Any>) {
        holder.uploadPic?.setOnClickListener {
            holder.itemView.context.startActivity(Intent(holder.itemView.context, UploadPicActivity::class.java)
                    .putExtra("orderId", item.order_id)
                    .putExtra("goodsId", item.goods_info[0].goods_id)
                    .putExtra("numLimit", item.goods_info[0].user_img_num_limit)
                    .putExtra("picList", item.uploadedList))
        }
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, item)
        } else {
            holder.uploadPic?.visibility = View.VISIBLE
            when {
                item.uploadedList.size == 0 -> holder.uploadPic?.text = "上传图片"
                item.uploadedList.size == item.goods_info[0].user_img_num_limit -> holder.uploadPic?.text = "查看图片"
                else -> holder.uploadPic?.text = "上传图片（已传${item.uploadedList.size}张）"
            }
        }
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: OrderDetailsBean) {
        viewHolder.update(bean)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var orderCount: TextView? = null
        var orderAmount: TextView? = null
        private var orderCode :TextView? = null
        private var orderTime :TextView? = null
        private var payPrice :TextView? = null
        var freight: TextView? = null
        var clMessage: ConstraintLayout? = null
        var message: TextView? = null
        var contactService: LinearLayout? = null
        var uploadPic: TextView? = null
        init {
            orderCount = itemView.findViewById(R.id.order_count)
            orderAmount = itemView.findViewById(R.id.order_amount)
            orderCode = itemView.findViewById(R.id.order_code)
            orderTime = itemView.findViewById(R.id.order_time)
            payPrice = itemView.findViewById(R.id.pay_price)
            freight = itemView.findViewById(R.id.freight)
            clMessage = itemView.findViewById(R.id.cl_message)
            message = itemView.findViewById(R.id.message)
            contactService = itemView.findViewById(R.id.contact_service)
            uploadPic = itemView.findViewById(R.id.upload_pic)
        }

        @SuppressLint("SetTextI18n")
        fun update(bean: OrderDetailsBean) {
            var count = bean.goods_info.size
            for (goodsInfo in bean.goods_info){
                if (goodsInfo.gift_set == 1) {
                    count += goodsInfo.gift_arr!!.buy_num
                }
            }
            orderCount?.text = "共${count}件商品，总价："
            orderAmount?.text = "¥ ${bean.total_price}"
            orderCode?.text = bean.order_code
            orderTime?.text = bean.date_ymd
            payPrice?.text = "¥${bean.pay_price}"
            freight?.text = "¥ ${bean.pay_freight}"
            if (!StringUtil.isBlank(bean.buyer_note)) {
                clMessage?.visibility = View.VISIBLE
                message?.text = bean.buyer_note
            }
            contactService?.setOnClickListener{
                itemView.context.startActivity(Intent(itemView.context, WebViewActivity::class.java)
                        .putExtra("url", Urls.URL_CUSTOM_SERVICE))
            }
        }
    }
}
